package LeetDev.a3d;

import javax.microedition.khronos.opengles.GL10;

public class DemoHandler extends EventHandler{

	Model m_shipModel;
	Node m_world[];
	GeomNode m_shipNode;
	float m_rotation=0;
	Quat m_shipRotation=new Quat();
	
	public DemoHandler()
	{
		super();
	}
	
	public void init(GL10 gl)
	{
		// Import models.
		Model.s_modelPath="/assets/";
//		Texture.s_texturePath="/res/drawable/";
		Texture.s_texturePath="/assets/";
		try
		{
//			m_shipModel=Model.create("goofy.gun", gl);
//			m_shipModel=Model.create("test.gun", gl);
//			m_shipModel=Model.create("slacker.gun", gl);
			m_shipModel=Model.create("buggy.gun", gl);
//			m_shipModel=Model.create("cube.gun", gl);
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
			System.out.println(e.getMessage());
			System.out.println("Import model failure.");
		}
		try
		{
			m_world=new Node[3];
			m_world[0]=new Camera(new Vect(5,8,12),new Vect(0,0,0),new Vect(0,1,0),60,1,100);
			m_world[1]=new Light(new Vect(0,1,0),new Color(1,1,1,1),-1,true);
			m_shipNode=new GeomNode(new Vect(0,0,0),m_shipModel);
			m_world[2]=m_shipNode;
			Engine.instance().setWorld(m_world);
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
			System.out.println(e.getMessage());
			System.out.println("World creation failure.");
		}
	}
	public void destruct(GL10 gl)
	{
		super.destruct(gl);
		Engine.instance().setWorld(null);
		m_shipNode=null;
		Model.destroy(m_shipModel, gl);
	}
	
	public void handleEvent(final Event event)
	{
		// It is safe to update any Node or model here. It will not collide with any drawing.
		// It is completely safe to change eventhandler in the engine at this moment.
		
	}
	
	public void frameEvent(GL10 gl, double time, double deltaTime)
	{
		m_rotation+=deltaTime*0.4;
		// Any continuous frame update should be placed here.
		gl.glClearColor(0.6f,0.6f,1,0);
//		m_shipNode.setScale(0.0004f,0.0004f,0.0004f);
		m_shipNode.setScale(0.4f,0.4f,0.4f);
		m_shipRotation.set(0,m_rotation,0);
		m_shipNode.setRotation(m_shipRotation);
//		m_shipNode.setScale(4,4,4);
	}

}
